//猫猫控制脚本
cc.Class({
    extends: cc.Component,

    properties: {
        //猫猫动作状态
        cat_action: {
            default: "",
            visible: false
        },
        //猫猫动作帧
        action_frame: {
            default: [],
            type: cc.SpriteFrame
        },
        //猫猫当前目标位置
        cat_arrive:{
            default: cc.p(0,0),
            visible: false
        },
        //猫猫当前移动角度
        cat_angel: {
            default: 0,
            visible: false
        },
        //猫猫上一次位置
        cat_last: {
            default: cc.p(0,0),
            visible: false
        },
        //猫猫移动速度
        cat_speed: 100,
    },

    onLoad () {
        Commond.Check_Screen(Game.Screen,this.node);
    },

    update (dt) {
        if(Game.State == GameState.End) {
            return;
        }
        if(this.cat_action == "die") {
            Game.State = GameState.End;
            Game.UI.getComponent("ui_control").show_result();
            return;
        }
        //若猫猫开始踢doge
        if(this.cat_action == "kick") {
            this.cat_last = this.vector_move(dt,this.cat_angel.dir);
            //若猫猫未到达屏幕边界则进行移动,若到达则强制恢复初始状态
            if(Commond.Check_Screen(Game.Screen,this.node) == false) {
                if(Commond.Check_Arrive(this.node,this.cat_arrive) == true) {
                    this.reset_state();
                }
            }else {
                //猫猫节点出界则退回之前位置[避免陷入死循环]
                this.node.x = this.cat_last.x;
                this.node.y = this.cat_last.y;
                this.reset_state();
            }
        }
    },

    //猫猫状态重置
    reset_state () {
        var anim = this.node.getComponent(cc.Animation);
        anim.play();  //恢复呼吸动画
        this.modfily_node("breath");
    },

    //猫猫恢复初始状态
    repeat_state () {
        var anim = this.node.getComponent(cc.Animation);
        anim.play();  //恢复呼吸动画
        this.cat_action = "";
        this.node.x = 0;
        this.node.y = 0;
    },

    //猫猫翻转方法
    //refer:参照点 dir:翻转方向[x代表左右 y代表上下]
    flip_cat (refe,dir) {
        var flip_dir = Commond.Direction_Check(this.node,refe,dir);
        if(dir == "x") {
            if(flip_dir == "right") {
                this.node.scaleX = -1;
            }
            if(flip_dir == "left") {
                this.node.scaleX = 1;
            }
        }

        if(dir == "y") {
            if(flip_dir == "bottom") {
                this.node.scaleY = -1;
            }
            if(flip_dir == "left") {
                this.node.scaleY = 1;
            }
        }
    },

    //修改猫猫动作状态
    //ac_type:动作类型  arrive:到达位置
    change_action (ac_type,arrive) {
        this.cat_action = ac_type;
        this.cat_arrive = arrive;
        //cc.log(this.cat_arrive);
    },

    //根据动作类型修改节点大小
    //[a_type:动作类型]
    modfily_node (a_type) {
        var cat = this.node;
        var width = 0;
        var height = 0;
        var frame = 0;
        switch(a_type) {
            case "kick":  //踢腿
                width = 48;
                height = 48;
                frame = Cat_Frame.KICK; 
            break;
            case "breath":  //呼吸[原地不动]
                width = 30;
                height = 46;
                frame = Cat_Frame.BREATH;
            break;
            case "die":  //扑街
                width = 50;
                height = 28;
                frame = Cat_Frame.DIE;
            break;
        }
        cat.width = width;
        cat.height = height;
        this.cat_action = a_type; //更新猫猫动作状态标记
        Commond.Change_Frame(this.node,this.action_frame[frame]);
    },

    //猫猫移动方法[动作系统法]
    //time:移动时间 pos:要移动的位置
    move_cat (time,pos) {
        var action = null;
        var distance = Commond.Distance_Point(this.node,pos);
        var anim = this.node.getComponent(cc.Animation);
        var callfun = cc.callFunc(function(){
            anim.play();  //恢复呼吸动画
            //恢复正确朝向
            this.node.scaleX = 1;
            this.node.scaleY = 1;
            this.modfily_node("breath");
        },this);
        //限制猫猫攻击距离
        if(distance > 30 && distance <= 300) {
            action = cc.moveTo(time,pos);
            anim.stop();  //停止呼吸动画
            this.modfily_node("kick");
            this.node.runAction(cc.sequence(
                action,callfun
            ));    
        }
        this.flip_cat(pos,"x");
    },

    //确定移动角度
    geting_angel (posA,posB) {
        var angel = 0;
        var deviation = {angel:0,dir:""}
        var slope = (posA.y - posB.y) / (posA.x - posB.x); //计算斜率
        this.node.getComponent(cc.Animation).stop();  //停止呼吸动画
        slope = Math.abs(parseFloat(slope.toFixed(2)));
        deviation.dir = Commond.Return_Direction(posA,posB);
        angel = parseFloat(Math.atan(slope).toFixed(2));  //利用反正切计算角度
        deviation.angel = parseInt(Commond.Transform_Radius(angel));
        this.flip_cat(posB,"x");  //翻转喵喵
        this.cat_angel = deviation;
        //cc.log(deviation.angel,deviation.dir);
    },

    //矢量移动[dt:移动间隔 dir:方向]
    vector_move(dt,dir) {
        var unix = cc.p(0,0);
        var last = this.node.getPosition();  //获取移动前的猫猫位置
        var angel = Math.abs(this.cat_angel.angel);
        var speed = parseFloat((this.cat_speed * dt).toFixed(2));
        switch(dir) {
            case "RT":
                angel = angel;
            break;
            case "LT":
                angel = 180 - angel;
            break;
            case "LB":
                angel = 180 + angel;
            break;
            case "RB":
                angel = 360 - angel;
            break;
        }
        unix = Commond.Return_Vector(angel,speed);
        this.node.x += unix.x;
        this.node.y += unix.y;
        return last;
    },

    
});
